package io.commchina.smart.member.provider.controller;

import io.commchina.platform.framework.exception.BizCodeEnume;
import io.commchina.platform.framework.sip.utils.PageUtils;
import io.commchina.platform.framework.tools.Result;
import io.commchina.smart.member.biz.MemberService;
import io.commchina.smart.member.entity.MemberEntity;
import io.commchina.smart.member.exception.PhoneExistException;
import io.commchina.smart.member.exception.UsernameExistException;
import io.commchina.smart.member.http.req.UserLoginReq;
import io.commchina.smart.member.http.req.UserRegisterReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 会员
 *
 * @author jnyou
 * @email xiaojian19970910@gmail.com
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    // 旧第三方登录方式
//    @PostMapping("/oauth2/login")
//    public Result oauthLogin(@RequestBody SocialUserReq user) {
//        MemberEntity memberEntity = memberService.oauthLogin(user);
//        if(null != memberEntity){
//            return Result.ok().put("memberEntity",memberEntity);
//        } else {
//            return Result.error(BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getCode(),BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getMsg());
//        }
//    }


    @PostMapping("/login")
    public Result login(@RequestBody UserLoginReq vo) {
        MemberEntity memberEntity = memberService.login(vo);
        if(null != memberEntity){
            return Result.ok().setData(memberEntity);
        } else {
            return Result.error(BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getCode(),BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getMsg());
        }
    }

    /**
     * 注册
     */
    @PostMapping("/regist")
    public Result regist(@RequestBody UserRegisterReq vo) {
        try {
            memberService.regist(vo);
        } catch (PhoneExistException e) {
            return Result.error(BizCodeEnume.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnume.PHONE_EXIST_EXCEPTION.getMsg());
        } catch (UsernameExistException e) {
            return Result.error(BizCodeEnume.USER_EXIST_EXCEPTION.getCode(), BizCodeEnume.USER_EXIST_EXCEPTION.getMsg());
        }
        return Result.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("memberservice:member:list")
    public Result list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return Result.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("memberservice:member:info")
    public Result info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return Result.ok().put("member", member);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("memberservice:member:save")
    public Result save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return Result.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@RequiresPermissions("memberservice:member:update")
    public Result update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return Result.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    //@RequiresPermissions("memberservice:member:delete")
    public Result delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return Result.ok();
    }

}
